package com.dailyyoga.view;
import com.tools.MultipleScreenTool;

import android.content.Context;
import android.util.Log;
import android.widget.Button;
import cn.domob.android.ads.DomobInterstitialAd;
import cn.domob.android.ads.DomobInterstitialAdListener;
import cn.domob.android.ads.DomobAdManager.ErrorCode;

public class MyInterstitialAd {

	public static final String InterstitialPPID_SESSION_INFO = "16TLmnFvApy1YNUHfhLiQRpz";
	public static final String InterstitialPPID_SESSION_PLAY = "16TLmnFvApy1YNUHfr5Jd6jk";

	DomobInterstitialAd mInterstitialAd;
	Button mInterstitialBtn;
	Context mContext;

	public MyInterstitialAd(Context context, String interstitialPPID) {
		mContext = context;
		mInterstitialAd = new DomobInterstitialAd(context,
				BannerAD.PUBLISHER_ID, interstitialPPID, getSize());
		initInterstitialAd();
	}

	public void initInterstitialAd() {
		mInterstitialAd
				.setInterstitialAdListener(new DomobInterstitialAdListener() {

					public void onInterstitialAdDismiss() {
						// TODO Auto-generated method stub
						// 在上一条广告关闭时,请求下一条广告。
						mInterstitialAd.loadInterstitialAd();
						Log.i("DomobSDKDemo", "onInterstitialAdDismiss");
					}

					public void onInterstitialAdFailed(ErrorCode arg0) {
						// TODO Auto-generated method stub
						Log.i("DomobSDKDemo", "onInterstitialAdFailed");
					}

					public void onInterstitialAdLeaveApplication() {
						// TODO Auto-generated method stub
						Log.i("DomobSDKDemo",
								"onInterstitialAdLeaveApplication");
					}

					public void onInterstitialAdPresent() {
						// TODO Auto-generated method stub
						Log.i("DomobSDKDemo", "onInterstitialAdPresent");
					}

					public void onInterstitialAdReady() {
						// TODO Auto-generated method stub
						Log.i("DomobSDKDemo", "onInterstitialAdReady");
					}

					public void onLandingPageClose() {
						// TODO Auto-generated method stub
						Log.i("DomobSDKDemo", "onLandingPageClose");
					}

					public void onLandingPageOpen() {
						// TODO Auto-generated method stub
						Log.i("DomobSDKDemo", "onLandingPageOpen");
					}

					public void onInterstitialAdClicked(DomobInterstitialAd arg0) {
						Log.i("DomobSDKDemo", "onInterstitialAdClicked");

					}
				});

	}

	public void loadAd() {
		mInterstitialAd.loadInterstitialAd();
	}

	public void showAd() {

		if (mInterstitialAd.isInterstitialAdReady()) {
			mInterstitialAd.showInterstitialAd(mContext);
		} else {
			mInterstitialAd.loadInterstitialAd();
		}

	}

	private String getSize() {

		String size = null;
		switch (MultipleScreenTool.getScreenCategory(mContext)) {

		case smallScreens:
		case normalScreens:
			size = DomobInterstitialAd.INTERSITIAL_SIZE_300X250;
			break;
		case xlargeScreens:
			size = DomobInterstitialAd.INTERSITIAL_SIZE_600X500;
			break;
		case largeScreens:
			size = DomobInterstitialAd.INTERSITIAL_SIZE_300X250;
			break;

		default:
			break;
		}
		return size;
	}
}
